package com.imdroid.carac.process;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

import com.imdroid.carac.IMDroid;
import com.imdroid.carac.model.Team;
import com.imdroid.carac.net.HttpHelper;

public class LocationProcess implements Runnable {
	private static final String TAG = "LocationProcess";
	/**
	 * The list of the location
	 */
	private static volatile ArrayList<Location> locations = new ArrayList<Location>();
	/**
	 * The thread of the sending the location
	 */
	private static Thread thread = null;

	/**
	 * Send the location
	 * 
	 * @param location
	 *            the location of devices
	 */
	public static void sendLocation(final Location location) {
		if (thread == null) {
			thread = new Thread(new LocationProcess());
			thread.start();
		}

		synchronized (locations) {
			locations.add(location);
			locations.notify();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		try {
			Thread.sleep(10000);
		} catch (Exception e) {
			e.printStackTrace();
		}

		Location location = null;
		while (true) {
			synchronized (locations) {
				while (locations.size() == 0) {
					try {
						locations.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				// Send the latest location ONLY
				location = locations.get(locations.size() - 1);
				locations.clear();
			}
			send(location);
		}
	}

	/**
	 * Set the location to other team member
	 * 
	 * @param location
	 *            the current location of device
	 */
	private static void send(Location location) {
		if (location == null) {
			LocationManager lm = (LocationManager) IMDroid.getContext()
					.getSystemService(Context.LOCATION_SERVICE);
			location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			if (location == null)
				lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

			if (location == null) {
				return;
			}
		}
		if (Team.getCurrentTeam() == null)
			return;

		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("userId", IMDroid.getUser()
				.getUser_id() + ""));
		params.add(new BasicNameValuePair("userName", IMDroid.getUser()
				.getUser_name() + ""));
		params.add(new BasicNameValuePair("teamId", Team.getCurrentTeam()
				.getTeamId() + ""));

		params.add(new BasicNameValuePair("lat", location.getLatitude() + ""));
		params.add(new BasicNameValuePair("log", location.getLongitude() + ""));
		params.add(new BasicNameValuePair("speed", location.getSpeed() + ""));
		try {
			String res = HttpHelper.execute("/i/TeamUserLocation", params);

			Log.d(TAG, "Send Location lat:" + location.getLatitude() + " lon:"
					+ location.getLongitude() + " speed:" + location.getSpeed()
					+ " reponse:" + res);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
